import { makeAutoObservable } from 'mobx'
import { getBarriers, getUserDossiersSend, getUsersLoginUserInfo, getUserJobRel } from './api'
import { useLocation } from 'umi'
import { getCookie } from '@/storage'
import type { jobItem } from './interface'

class Store {
    barriersInfo: any = {}
    auth_token: string = ''
    userInfo: any = {}
    jobList: jobItem[] = []

    constructor() {
        makeAutoObservable(this)
        this.setAuth()
    }

    /** 免登token */
    setAuth() {
        // eslint-disable-next-line react-hooks/rules-of-hooks
        const { auth_token } = useLocation().query
        this.auth_token = auth_token
    }

    /** 获取档案信息 */
    getBarriersInfo = (id: string) => {
        getBarriers(id, this.auth_token ? { auth_token: this.auth_token } : null).then(({ data }) => {
            this.barriersInfo = data
            document.title = data?.name || ''
        })
    }

    /** 获取用户信息 */
    getUserInfo = async () => {
        const { data } = await getUsersLoginUserInfo()
        this.userInfo = data
    }

    /** 发送pdf */
    sendPdf = async (email?: string) => {
        return await getUserDossiersSend({
            url: `${window.location.origin}/#/template/myArchives?token=${getCookie('TOKEN')}`,
            email,
        })
    }

    resetBarriers = () => {
        this.barriersInfo = {}
    }

    /** 获取意向列表 */
    getUserJobRel = () => {
        return getUserJobRel().then((res: any) => {
            const { data, status } = res
            if (status !== 200) return
            this.jobList = data
        })
    }
}
export default Store
